class RangeAddQueries {
//   public static int[][] rangeAddQueries(int n, int[][] queries) {
//     int[][] res = new int[n][n];
//     for (int[] query : queries) {
//       int r1 = query[0], c1 = query[1], r2 = query[2], c2 = query[3];
//       for(int i = r1; i <= r2; i++){
//         for(int j = c1; j <= c2; j++){
//           res[i][j]++;
//         } 
//       }
//     }
//     return res;
//   }

    public int[][] rangeAddQueries(int n, int[][] queries) {
        // 二维差分
        int[][] diff = new int[n + 2][n + 2];
        for (int[] q : queries) {
            int r1 = q[0], c1 = q[1], r2 = q[2], c2 = q[3];
            diff[r1 + 1][c1 + 1]++;
            diff[r1 + 1][c2 + 2]--;
            diff[r2 + 2][c1 + 1]--;
            diff[r2 + 2][c2 + 2]++;
        }

        // 原地计算 diff 的二维前缀和，然后填入答案
        int[][] ans = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                diff[i + 1][j + 1] += diff[i + 1][j] + diff[i][j + 1] - diff[i][j];
                ans[i][j] = diff[i + 1][j + 1];
            }
        }
        return ans;
    }



  public static void main(String[] args) {
    int n = 3;
    int[][] queries = {{0,0,2,2},{0,0,1,0},{2,2,2,2}};
    RangeAddQueries solution = new RangeAddQueries();
    int[][] res = solution.rangeAddQueries(n, queries);
    for (int i = 0; i < res.length; i++) {
      for (int j = 0; j < res[i].length; j++) {
        System.out.print(res[i][j] + " ");
      }
    
    }
  }
}
  
